package com.sl.au.service.impl;

import com.sl.au.entity.AfterSaleComplain;
import com.sl.au.entity.KeyContact;
import com.sl.au.entity.ProductMess;
import com.sl.au.repository.AfterSaleComplainRepository;
import com.sl.au.repository.KeyContactRepository;
import com.sl.au.repository.ProductMessRepository;
import com.sl.au.service.AfterSaleComplainService;
import com.sl.au.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

//import org.apache.poi.util.StringUtil;

@Service("AfterSaleComplainService")
@Transactional
public class AfterSaleComplainServiceImpl extends BaseServiceImpl implements AfterSaleComplainService {

    @Autowired
    AfterSaleComplainRepository afterSaleComplainRepository;

    @Autowired
    ProductMessRepository productMessRepository;

    @Override
    public ProductMessRepository getProductMessRepository() {
        // TODO Auto-generated method stub
        return this.productMessRepository;
    }

    @Override
    public List<AfterSaleComplain> getComplain(int start, int length, String key, String orderName, String dir) {
        Page<AfterSaleComplain> pages = null;
        Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
        if (StringUtils.hasText(key)) {
            pages = afterSaleComplainRepository.findComplain(key, pageable);
        } else {
            pages = afterSaleComplainRepository.findAll(pageable);
        }
        return pages.getContent();
    }

    @Override
    public List<ProductMess> getProductMess(int start, int length, String key, String complainId) {
        Page<ProductMess> pages = null;
        Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
        if(StringUtils.hasText(key)){
            pages = productMessRepository.findProductMess(complainId, key, pageable);
        }else{
            pages=productMessRepository.findProductMess(complainId, pageable);
        }
        return pages.getContent();
    }


    public AfterSaleComplainRepository getRepository() {
        return this.afterSaleComplainRepository;
    }

}

